package com.ems.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ems.entity.Question;
import com.ems.exception.Result;
import com.ems.exception.ResultCode;
import com.ems.service.TeacherService;
import com.ems.service.impl.TeacherServiceImpl;
import org.apache.ibatis.javassist.runtime.Inner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;


//  1/20宋(在最顶端表示，该类为宋建立)
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    


    @RequestMapping(value = "/examMsgPost",method = RequestMethod.POST)
    public Object putExam(@RequestBody JSONObject json){
        if(json != null){
            //只是把json放进去，处理json在impl层使用
            if(teacherService.putExam(json)){
                return Result.ok();
            }
            return Result.no();
        }
        return Result.state(ResultCode.PARAM_ERROR);
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Object register(@RequestBody JSONObject json){
        if(json != null){
            if(teacherService.register(json)){
                return Result.ok();
            }
            return Result.no();
        }
        return Result.state((ResultCode.PARAM_ERROR));
    }

    @RequestMapping(value = "/createExamPaper",method = RequestMethod.POST)
    public Object createExamPaper(@RequestBody JSONObject json){
        if (json != null){
            return Result.ok().data(teacherService.creatExamPaper(json));
        }
        return Result.state((ResultCode.PARAM_ERROR));
    }
    // 王：2-13
    @RequestMapping(value = "/delExam",method = RequestMethod.DELETE)
    public Object delExam(Integer teaid,Integer examid){
        if (teaid != null && examid != null){
            if(teacherService.delExam(examid,teaid))
            {
                return Result.ok();
            }
            return Result.no();
        }
        return Result.state(ResultCode.NULLPOINTER);
    }

    @RequestMapping(value = "/examMsgPut",method = RequestMethod.GET)
    public Object examInfo(String username){
        System.out.println(username);
        return Result.ok().data(teacherService.getExamInfo(username));
    }

    @RequestMapping(value = "/examMsgInvi",method = RequestMethod.GET)
    public Object inviExamInfo(String username){
        System.out.println(username);
        return Result.ok().data(teacherService.inviTeacherId(username));
    }
    @RequestMapping(value = "/queryQuestion-subjectiveQuestions",method = RequestMethod.GET)
    public Object queryQuestion_subjectiveQuestions(String subject,Integer difficulty,String point){
        if(subject != null){
            return Result.ok().data(teacherService.queryQuestion_subjectiveQuestions(subject,difficulty,point));
        }
        return Result.state((ResultCode.PARAM_ERROR));
    }

    @RequestMapping(value = "/queryQuestion-singleChoice",method = RequestMethod.GET)
    public Object queryQuestion_singleChoice(String subject,Integer difficulty,String point){
        if(subject != null){
            return Result.ok().data(teacherService.queryQuestion_singleChoice(subject,difficulty,point));
        }
        return Result.state((ResultCode.PARAM_ERROR));
    }

    @RequestMapping(value = "/queryQuestion-multipleChoice",method = RequestMethod.GET)
    public Object queryQuestion_multipleChoice(String subject,Integer difficulty,String point){
        if(subject != null){
            return Result.ok().data(teacherService.queryQuestion_multipleChoice(subject,difficulty,point));
        }
        return Result.state((ResultCode.PARAM_ERROR));
    }

    @RequestMapping(value = "/markPaper",method = RequestMethod.GET)
    public Object markPaper(Integer eid){
        if (eid != null)
        {
            TeacherServiceImpl.RES studentPaper = (TeacherServiceImpl.RES) teacherService.getStudentPaper(eid);
            if (studentPaper.isIsnull()) return Result.state(ResultCode.HAS_FINISH);
            else if (studentPaper.isException()) return Result.no();
            return Result.ok().data(studentPaper.getData());

        }
        return Result.state(ResultCode.NULLPOINTER);
    }

    @RequestMapping(value = "/studentScorePut",method = RequestMethod.POST)
    public Object studentScorePut(@RequestBody JSONObject jsonObject){
        if (jsonObject != null){
            return Result.ok().data(teacherService.studentScorePut(jsonObject));
        }
        return Result.state(ResultCode.NULLPOINTER);
    }


    @RequestMapping(value = "/getExamPaper",method = RequestMethod.GET)
    public Object getExamPaper(Integer eid){
        if (eid != null){
            return Result.ok().data(teacherService.getExamPaper(eid));
        }
        else {
            return Result.state((ResultCode.PARAM_ERROR));
        }
    }

    @RequestMapping(value = "/checkChoice",method = RequestMethod.PUT)
    public Object checkChoice(@RequestBody JSONObject jsonObject){
        if (jsonObject != null){
            TeacherServiceImpl.RES res = (TeacherServiceImpl.RES) teacherService.calculateScore(jsonObject);
            if (res.isException() && !res.isIsnull()) return Result.state(ResultCode.HAS_FINISH);
            if(res.isIsnull()) return Result.state(ResultCode.NULLPOINTER);
            return Result.ok();
        }
        return Result.state(ResultCode.NULLPOINTER);
    }

    @RequestMapping(value = "/examAnalyze",method = RequestMethod.GET)
    public Object examAnalyze(Integer eid){
        if(eid != null){
            return Result.ok().data(teacherService.examAnalyze(eid));
        }
        return Result.state(ResultCode.PARAM_ERROR);
    }

    //给题库添加题目
    @RequestMapping(value = "/putQuestion",method = RequestMethod.POST)
    public Object putQuestion(Question question){
       teacherService.putQuestion(question);
       return Result.ok();
    }


}
